﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Solution102
{
    public IList<IList<int>> LevelOrder(TreeNode root)
    {


        Queue<TreeNode> queue = new Queue<TreeNode>();
        IList<IList<int>> res = new List<IList<int>>();
        if (root == null)
        {
            return res;
        }
        queue.Enqueue(root);
        while (queue.Count > 0)
        {
            //不在入队的时候收集，也不在出队的时候收集

            int curLen = queue.Count;
            IList<int> list = new List<int>();

            for(int i=0; i<curLen; i++)
            {

                TreeNode cur = queue.Dequeue();

                list.Add(cur.val); 


                if (cur.left != null)
                {
                    queue.Enqueue(cur.left);
                }
                if (cur.right != null)
                {
                    queue.Enqueue(cur.right);
                }
            }
            res.Add(list);


        }

        return res;

    }
}
class Program102 {


static void Main102(string[] args)
    {


        Solution102 solution = new Solution102();
        TreeNode root=new TreeNode(1);
        IList<IList<int>> res= solution.LevelOrder(root);
        for (int i = 0; i < res.Count; i++)
        {
            for (int j = 0; j < res[i].Count; j++)
            {


                Console.Write(res[i][j] + ",");
            }
            Console.WriteLine();
        }
    }

}
